package cn.yeyiu.sexyiu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.VideoUtil;
import cn.yeyiu.util.bean.VideoInfo;
import cn.yeyiu.videoplayer.VideoPlayerActivity;

public class RandomVideoAdapter extends RecyclerView.Adapter<RandomVideoAdapter.ViewHolder> {
    private final Context mContext;
    public final MutableLiveData<List<VideoInfo>> dataListLd;
    private final LayoutInflater inflater;


    public void updateVideo(){
        VideoUtil.postAnyRandomVideoList(dataListLd);
    }

    public RandomVideoAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.dataListLd = new MutableLiveData<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View root = this.inflater.inflate(R.layout.item_data, viewGroup, false);
        return new ViewHolder(root);
    }

    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        List<VideoInfo> list = this.dataListLd.getValue();
        VideoInfo videoInfo = list.get(i);
        MediaUtil.fixLocalVideoInfo(videoInfo);

        viewHolder.title.setText(videoInfo.title);
        Glide.with(this.mContext)
                .load(videoInfo.picSrc)
                .placeholder(R.drawable.jpg_home_header)
                .into(viewHolder.image);

        viewHolder.itemView.setOnClickListener(v -> {
            MediaUtil.playVideo(mContext, VideoPlayerActivity.class, videoInfo);
        });

    }

    @Override
    public int getItemCount() {
        List<VideoInfo> list = this.dataListLd.getValue();
        if (list == null) {
            return 0;
        }
        return list.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;

        public ViewHolder(View view) {
            super(view);
            this.image = (ImageView) view.findViewById(R.id.data_pic_img);
            this.title = (TextView) view.findViewById(R.id.data_title_tv);
        }
    }
}
